# -*- coding: utf-8 -*
from flask import request, jsonify
from flask_login import login_required

from conf.flask_ini import FlaskIni
from medical_kg.kg_alter import KgAlter
from medical_kg.prop_set_up import PropSetUp
from web.const import CONTEXT, StatusCode
from web.validation import valid

app = FlaskIni().get_app()
rbac = FlaskIni().get_rbac()
kg_alter_app = KgAlter()
prop_set_up_app = PropSetUp()

prefix = f'{CONTEXT}/kg_alter'


@app.route(f'{prefix}/query_prop', methods=['POST'])
@rbac.allow(roles=['小萌新'], methods=['POST'])
@login_required
def query_prop():
    data = prop_set_up_app.query_prop()
    return jsonify({'status': StatusCode.SUCCESS_CODE,
                    'message': '',
                    'payload': data})


@app.route(f'{prefix}/query_rel_type_list', methods=['POST'])
@rbac.allow(roles=['小萌新'], methods=['POST'])
@login_required
def query_rel_type_list():
    start_type_name = request.json['start_type_name']

    valid(start_type_name)

    rel_type_list = kg_alter_app.query_rel_type_list(start_type_name)
    return jsonify({'status': StatusCode.SUCCESS_CODE,
                    'message': '',
                    'payload': {
                        'rel_type_list': rel_type_list
                    }})


@app.route(f'{prefix}/alter_gragh', methods=['POST'])
@rbac.allow(roles=['编辑员'], methods=['POST'])
@login_required
def alter_gragh():
    uid = request.json['uid']
    graph = request.json['graph']

    valid(uid)

    kg_alter_app.alter_gragh(uid, graph)
    return jsonify({'status': StatusCode.SUCCESS_CODE,
                    'message': '',
                    'payload': {}})
